import minidump from 'minidump'
import fs from 'fs-extra'
import path from 'path'

export async function decompress(symbolPath, dumpPath, destDir) {
  return new Promise((resolve, reject) => {
    minidump.addSymbolPath(symbolPath)
    minidump.walkStack(dumpPath, (err, result) => {
      if (err) reject(err)
      if (!destDir) {
        destDir = path.dirname(dumpPath)
      }
      const destPath = path.join(destDir, path.basename(dumpPath) + '.txt')
      fs.writeFile(destPath, result.toString(), (err) => {
        if (err) reject(err)
        resolve(undefined)
      })
    })
  })
}
